package com.xgkj.diyiketang.activity;


import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewCompat;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.umeng.analytics.MobclickAgent;
import com.xgkj.diyiketang.EatShowApp;
import com.xgkj.diyiketang.util.ACache;

import java.lang.reflect.Field;


public class BaseActivity extends FragmentActivity {
    protected Context mContext;
//    protected ACache mACache;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        mContext = this;
//        mACache = ACache.get(this);
        getSharePrefence();
//		MobclickAgent.setDebugMode(true);
        getEatShowApp().AddContextStack(mContext);
        /*  try {
            Object window = getWindow();
            Class<?> cla = window.getClass();
            Method method = cla.getDeclaredMethod("setStatusBarIconDark",boolean.class);
            method.invoke(window,true);
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("baseActivity:::","Exception::"+e);
        }*/
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            getWindow().setStatusBarColor(Color.parseColor("#E5FAFAFA"));

        }
    }
    public void getSharePrefence(){
        SharedPreferences sharedPreferences= getSharedPreferences("test",
                Activity.MODE_PRIVATE);
        //实例化SharedPreferences.Editor对象
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.commit();
    }
    public static void apiNinetyStatus (Activity activity) {
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
            //使用SystemBarTint库使4.4版本状态栏变色，需要先将状态栏设置为透明
            /*//transparencyBar(this);
             getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            SystemBarTintManager tintManager = new SystemBarTintManager(this);
            tintManager.setStatusBarTintEnabled(true);
            //tintManager.setStatusBarTintResource();
            tintManager.setStatusBarTintColor(Color.parseColor("#ff0000"));*/

            Window window = activity.getWindow();
            ViewGroup mContentView = (ViewGroup)activity.findViewById(Window.ID_ANDROID_CONTENT);

            if (mContentView == null)
                Log.e("mContentView::","mcontentView是空");
            //First translucent status bar.
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //int statusBarHeight = getStatusBarHeight(this);
            Class c = null;
            Object obj = null;
            Field field = null;
            int x = 0;
            int statusBarHeight = 0;
            try {
                c = Class.forName("com.android.internal.R$dimen");
                obj = c.newInstance();
                field = c.getField("status_bar_height");
                x = Integer.parseInt(field.get(obj).toString());
                statusBarHeight = activity.getResources().getDimensionPixelSize(x);
                //statusBarHeight = 500;
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
            Log.e("statusBarHeight::",""+statusBarHeight);
            View mTopView = mContentView.getChildAt(0);
            Log.e("childview的个数：：",mContentView.getChildCount()+"");
            if (mTopView != null && mTopView.getLayoutParams() != null && mTopView.getLayoutParams().height == statusBarHeight) {
                //避免重复添加 View
                Log.e("baseactivity","1");
                mTopView.setBackgroundColor(Color.parseColor("#FAFAFA"));
                return;
            }
            //使 ChildView 预留空间
            if (mTopView != null) {
                Log.e("baseactivity","2");
                ViewCompat.setFitsSystemWindows(mTopView, true);
            }

            //添加假 View
            mTopView = new View(activity);
            ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusBarHeight);
            mTopView.setBackgroundColor(Color.parseColor("#FAFAFA"));
            mContentView.addView(mTopView, 0, lp);


        }
    }

    @TargetApi(19)
    public static void transparencyBar(Activity activity){
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = activity.getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.TRANSPARENT);
        } else
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window =activity.getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

            /*添加主题*/
            //window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

        }
    }

    protected EatShowApp getEatShowApp() {
        return (EatShowApp) getApplication();
    }

    protected void onDestroy() {
        super.onDestroy();
        if (getApplication() != null)
            ((EatShowApp) getApplication()).removeContext(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(mContext);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(mContext);
    }


}
